/**
 * 文件工具类
 */

/**将图片base64转换为File/Blob (binary long object)，用于multipart/form-data提交*/
function base64toFile (base64, filename = "") {
    let arr = base64.split(","),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, {
        type: mime
    });
}
/**将File转换成图片，显示*/
function fileToBase64 (blob) {
    return new Promise((resolve, reject) => {
        let reader = new FileReader();
        reader.readAsDataURL(blob);
        reader.onload = function () {
            resolve(reader.result);
        };
    })
}

function fileToUrl (blob) {
    return URL.createObjectURL(blob);
}


export default {
    base64toFile,
    fileToBase64,
    fileToUrl,
}
